<template>
  <el-card>
    <!-- 搜索区域 -->
    <div class="search_box">
      <el-form ref="searchFormRef" :model="queryInfo" label-width="120px" class="search-box">
        <el-row :gutter="24">
          <!-- <el-col :span="6">
            <el-form-item prop="user_name" label="名称:">
              <el-input size="large" v-model.trim="queryInfo.username" placeholder="搜索名称" :prefix-icon="Search"
                @change="resetTable"></el-input>
            </el-form-item>
          </el-col> -->

          <el-col :span="24">
            <div class="flex justify-end align-center">
              <!-- <el-button type="info" :icon="RefreshRight" size="large" @click="resetTable">重置</el-button> -->

              <el-button type="danger" size="large" @click="openAddressForm({}, 'add')">新增地址</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="margin-top">
      <el-table ref="multipleTableRef" :data="listData" style="width: 100%">
<!--        <el-table-column label="名称" property="username"></el-table-column>-->

<!--        <el-table-column label="省" property="province"></el-table-column>-->

<!--        <el-table-column label="市" property="city"></el-table-column>-->

<!--        <el-table-column label="区" property="county"></el-table-column>-->

        <el-table-column label="详细地址" property="address"></el-table-column>

<!--        <el-table-column label="联系电话" property="phone"></el-table-column>-->

        <el-table-column label="操作">
          <template #default="scope">
            <div class="flex align-center">
              <el-button class="margin-left-xs" size="small" plain type="default"
                @click="openAddressForm(scope.row, 'edit')">编辑</el-button>

              <el-button class="margin-left-xs" size="small" plain type="danger"
                @click="handleDelete(scope.row, 'edit')">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <!-- <div class="margin-top">
      <Pagination :total="total" @change="changePage" />
    </div> -->

    <AddressFormModal ref="addressFormMoalRef" @reload="resetTable" />
  </el-card>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, toRef, onMounted, watch } from 'vue'
import Select from '@/components/admin/Select.vue'
import { useRoute } from 'vue-router'
import { useRouter } from 'vue-router';
import useTableHook from '@/hooks/useTableHook'
import { GetPayAndEditApi, SetPayStatusApi, GetAddressAddAndEditApi } from '@/api/index'
import { RefreshRight } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Pagination from '@/components/admin/Pagination.vue'
import AddressFormModal from './AddressFormModal.vue';

const addressFormMoalRef = ref()

const openAddressForm = (record, type) => {
  addressFormMoalRef.value.open({
    data: {
      ...record
    }
  }, type)
}

const handleDelete = (record) => {
  ElMessageBox.confirm('确认删除地址', '删除', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  }).then(async (res) => {
    try {
      const res = await GetAddressAddAndEditApi({
        method: 'delete',
        addressId: record.id,
        id: record.id,
      })


      ElMessage({
        type: 'success',
        message: '删除成功!'
      })

      resetTable()
    } catch (error) {
      ElMessage({
        type: 'error',
        message: '删除失败!'
      })
    }
  })
}

const queryInfo = reactive({
  method: 'list',
  username: ''
})

const searchFormRef = ref()

const listData = ref([])

const getAddressDataRequest = async () => {
  try {
    const res = await GetAddressAddAndEditApi(queryInfo)

    listData.value = res.data
  } catch (error) {
    ElMessage({
      type: 'error',
      message: '获取数据失败!'
    })
  }
}

// 重置表格
const resetTable = () => {
  // searchFormRef.value.resetFields()

  getAddressDataRequest()
}

onMounted(() => {
  getAddressDataRequest()
})



</script>

<style lang="scss">
$primary: #3498db;

/* 头部区域样式 */
/* 页面标题区域 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  background-color: white;
  padding: 20px 24px;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
}

.page-header h1 {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
  position: relative;
}

.page-header h1::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -8px;
  width: 40px;
  height: 3px;
  background-color: $primary;
  border-radius: 3px;
}

.btn-create {
  background-color: $primary;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-create:hover {
  background-color: darken($primary, 10%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(66, 153, 225, 0.3);
}

.btn-create::before {
  content: '+';
  font-size: 18px;
  font-weight: 500;
}

.title-section {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  color: #2c3e50;
}

.merchant-name {
  color: #e74c3c;
}

.back-link {
  display: inline-flex;
  align-items: center;
  font-size: 14px;
  color: #7f8c8d;
  text-decoration: none;
  transition: color 0.3s;
  margin-top: 5px;
}

.back-link:hover {
  color: #e74c3c;
}

.back-link i {
  margin-right: 5px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.btn-primary {
  background-color: #e74c3c;
  color: white;
  border: none;
  padding: 10px 15px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  font-weight: 600;
  transition: background-color 0.3s;
}

.btn-primary:hover {
  background-color: #c0392b;
}

.btn-primary i {
  margin-right: 5px;
}
</style>
